$(function () {
    obj.init();
});

var obj = new function () {
    return {
        form: null,
        clickFlag: false,
        List : [],
        init: function () {
            this.form = new xhForm($('#xh-form'));
            this.form.parse();
            this.funObj().addData();//获取select数据
            this.listener();
        },
        listener: function () {
            var $this = this;
            var clickFlag = this.clickFlag;
            var List = this.List;
            $('#school').unbind('change').on('change',function () {
                $this.funObj().addTeacherData();
            });
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().saveData();
            });
            var count = 0; //记录上传次数
            var fileInput = $("#fileInput");
            fileInput.on("fileuploaded", function (event, data, previewId, index) {
                count++;
                List.push({ FileName: data.response.msg.fileName, KeyID: previewId, url:data.response.msg.url });
                if(clickFlag && (count === data.files.length)){
                    clickFlag = false;
                    count = 0;
                    save();
                }
            });

            fileInput.on("filesuccessremove", function (event, data, previewId, index) {
                for (var i = 0; i < List.length; i++) {
                    if (List[i].KeyID == data) {
                        List.splice(i,1);
                    }
                }
            });
        },
        funObj: function () {
            var form = this.form;
            var $this = this;
            return {
                addData: function () {
                    xhExtra.ajaxRequest('bpm/operate_pro/getSchoolListData', {}, {check:true}, function (data) {
                        $('#school').select2({placeholder: '选择学校', data: data, allowClear: true});
                    });
                    $("[id=startTime]").datetimepicker({
                        format: 'yyyy-mm-dd',
                        language: 'zh-CN',
                        minView: "month",
                        autoclose: true
                    });
                    $('#temp').select2({placeholder: '选择模板', allowClear: true});
                    $('#teacher').select2({placeholder: '选择老师', allowClear: true});
                    $("#fileInput").fileinput('destroy').fileinput({
                        language: 'zh', //设置语言
                        uploadUrl: 'updateFile', //上传的地址
                        allowedFileExtensions : ['jpg', 'jpeg', 'png', 'gif', 'bpm', 'xlsx','docx', 'doc', 'pdf','xls'],//接收的文件后缀
                        uploadAsync: true, //默认异步上传
                        showUpload: true, //是否显示上传按钮
                        showCaption: false,//是否显示标题
                        dropZoneEnabled: false,
                        browseClass: "btn btn-primary", //按钮样式
                        maxFileSize: 50000,
                        maxFileCount: 10, //表示允许同时上传的最大文件个数
                        enctype: 'multipart/form-data',
                        validateInitialCount:true,
                        height:200,
                        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
                        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！"
                    });
                },
                addTeacherData: function () {
                    var schoolId = $('#school').val();
                    $("#teacher").find("option").remove();
                    xhExtra.ajaxRequest('bpm/operate_pro/getTeaDataBySchool', {schoolId:schoolId}, {check:true}, function (data) {
                        $('#teacher').select2({placeholder: '选择教师',data: data,allowClear: true,multiple : true});
                    });
                },
                saveData: function () {
                    var school = $('#school').val();
                    var temp = $('#temp').val();
                    var startTime = $('#startTime').val();
                    var remark = $('#remark').val();
                    var teacher = $('#teacher').val();
                    var List = $this.List;
                    var FileUrl = '';
                    if (!school || !temp || !startTime){
                        window.top.xhTop.showError('请填写名称、学校、模板和时间信息');
                        xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        return false;
                    }
                    if (JSON.stringify(List) !== '[]'){
                        for (var i = 0; i < List.length; i++) {
                            FileUrl += List[i]['url']+',';
                        }
                    }

                    form.submit('bpm/operate_pro/saveOperateProData',{school:school, temp:temp, startTime:startTime, remark:remark, teacher:teacher, fileUrl:FileUrl},function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        window.top.xhTop.getActiveFrame().obj.table.treegrid('reload');
                        window.top.xhTop.closeModal();
                    });
                }
            }
        }
    }
};
